//
//  SettingViewController.m
//  YunGui
//
//  Created by wmm on 16/4/8.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "SettingViewController.h"
#import "IRevealControllerProperty.h"
#import "SideMenuUtil.h"
#import "NormalItemCell.h"
#import "OutLoginCell.h"
#import "ViewController.h"

@interface SettingViewController ()<IRevealControllerProperty,UITableViewDataSource, UITableViewDelegate,OutLoginCellDelegate>
{
    UITableView    *_settingTableView;
    
}
@property (nonatomic, strong) NSMutableArray *itemArray;

@end

@implementation SettingViewController

@synthesize revealController;

- (void)viewDidLoad {
    [super viewDidLoad];
    [SideMenuUtil addViewGesture:self revealController:revealController];
    
    //set NavigationBar 背景颜色&title 颜色
    [ViewTool setNavigationBar:self.navigationController.navigationBar];
    self.navigationItem.title = @"设置";
    self.navigationItem.leftBarButtonItem=[ViewTool getListBarButtonItemWithTarget:self WithAction:@selector(sideLeftButton_selector:)];
    self.view.backgroundColor = GrayBackColor;
    
    [self initTableView];
}

// 拉开左侧:点击.
- (void)sideLeftButton_selector:(id)sender {
    
    NSLog(@"SubdistrictViewController...");
    [self.revealController toggleSidebar:!self.revealController.sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
}

#pragma mark --lazyload
- (NSMutableArray *)itemArray
{
    if (_itemArray == nil) {
        _itemArray = [NSMutableArray arrayWithArray:@[@[@"版本信息"],@[@"关于我们"],@[@"退出登录"]]];
    }
    
    return _itemArray;
}
#pragma mark --tableview部分
/**
 *  初始化tableView
 */
- (void)initTableView
{
    
    _settingTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _settingTableView.delegate = self;
    _settingTableView.dataSource = self;
    
    _settingTableView.scrollEnabled = NO;
    _settingTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    _settingTableView.backgroundColor = GrayBackColor;
    [self.view addSubview:_settingTableView];
    
}

#pragma makr -- tableview的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //    NSLog(@"%ld",_itemArray.count);
    return self.itemArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [self.itemArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NormalItemCell *cell = [NormalItemCell cellWithTableView:tableView];
    cell.backgroundColor = [UIColor whiteColor];
    if(indexPath.section == 0 ){
        UIImage *icon = [UIImage imageNamed:@"Btn_Nor_Banbengengxin"];
        CGSize itemSize = CGSizeMake(25, 25);
        UIGraphicsBeginImageContextWithOptions(itemSize, NO,0.0);
        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        [icon drawInRect:imageRect];
        
        cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        cell.textLabel.text = self.itemArray[indexPath.section][indexPath.row];
        cell.textLabel.textColor = BLACKCOLOR;
        cell.textLabel.font = [ViewTool getFont:15.0f];
        //        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
        
    }else if (indexPath.section == 1){
        UIImage *icon = [UIImage imageNamed:@"Btn_Nor_Guanyuwomen"];
        CGSize itemSize = CGSizeMake(25, 25);
        UIGraphicsBeginImageContextWithOptions(itemSize, NO,0.0);
        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        [icon drawInRect:imageRect];
        
        cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        cell.textLabel.text = self.itemArray[indexPath.section][indexPath.row];
        cell.textLabel.textColor = BLACKCOLOR;
        cell.textLabel.font = [ViewTool getFont:15.0f];
        //        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }else if (indexPath.section == _itemArray.count - 1){
        OutLoginCell *cell1 = [OutLoginCell cellWithTableView:tableView];
        cell1.textLabel.text = self.itemArray[indexPath.section][indexPath.row];
        cell1.selectionStyle = UITableViewCellSelectionStyleNone;
        cell1.backgroundColor = [UIColor clearColor];
        cell1.delegate = self;
        cell.backgroundColor = [UIColor whiteColor];
        return cell1;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.section == 0 && indexPath.row == 0 ){
        
        [Tools showMessage:@"云柜 version 1.0" view:self];
        
    }else if ( indexPath.section == 1 && indexPath.row == 0){
        [Tools showMessage:@"Copyright 2008-2016 南京汉恩 版权所有" view:self];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 15;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15)];
    view.backgroundColor = [UIColor clearColor];
    
    UIView *lineView = [[UIView alloc] init];
    [view addSubview:lineView];
    lineView.frame = CGRectMake(0, 14, kScreenWidth, 1);//不能用self.frame.size.height 会一直使用未返回的cell默认高度 44
    lineView.backgroundColor = [UIColor colorWithRed:236.0 / 255 green:235.0/255 blue:232.0/255 alpha:1];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section != self.itemArray.count - 1) {
        return [NormalItemCell cellHeight];
    }
    else{
        return [OutLoginCell cellHeight];
    }
    return 44;
}

- (void)clickOutLogin
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:@"roleId"];
    [userDefaults removeObjectForKey:@"roleName"];
    [userDefaults removeObjectForKey:@"roleTag"];
    [userDefaults removeObjectForKey:@"userId"];
    [userDefaults removeObjectForKey:@"userName"];
    
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [dictionary setObject:@"app.user.login.out" forKey:@"method"];
    [dictionary setObject:@"yeguan" forKey:@"app_key"];
    [dictionary setObject:@"json" forKey:@"format"];
    [dictionary setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"userToken"] forKey:@"userToken"];
    [DataTool postWithUrl:HEAD_URL parameters:[DataTool addSignToDic:dictionary] success:^(id data) {
        [userDefaults removeObjectForKey:@"userToken"];
        ViewController *viewController = [[ViewController alloc] init];
        [self presentViewController:viewController animated:YES completion:nil];
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSString *status = [(NSDictionary *)jsonDic objectForKey:@"status"];
        NSLog(@"%@",jsonDic);
        if ([status isEqualToString:@"SUCCESS"]) {
            NSLog(@"登出成功");
        }else{
//            [Tools showMessage:@"登出失败" view:self];
            NSLog(@"登出失败");
        }
    } fail:^(NSError *error) {
        NSLog(@"error:%@",error);
        [Tools showMessage:@"登出失败" view:self];
    }];
    
    [userDefaults synchronize];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
